package com.stylefeng.guns.modular.huamao.util;

import cn.hutool.core.util.RandomUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.*;

/**
 * sulong
 */
public class IMUtils {

/*
    private static final String CREAT_URL = "https://api.netease.im/nimserver/user/create.action";
    private static final String SENDMSG = "https://api.netease.im/nimserver/msg/sendMsg.action";
    private static final String APPKEY = "66da7f568843043ea844b8084b961193";
    private static final String APPSECRET = "a5f4f4fc5407";

    *//**
     * 创建网易云通信ID
     *
     * @return
     * @throws Exception
     *//*
    public static JSONObject common(String url, Map<String, String> map) throws Exception {
//        DefaultHttpClient httpClient = new DefaultHttpClient();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 执行请求
        HttpResponse response = httpClient.execute(getHttpPost(url, map));
        String s = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }




    public static HttpPost getHttpPost(String url, Map<String, String> map) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        String nonce = RandomUtil.randomString(6);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(APPSECRET, nonce, curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", APPKEY);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair basicNameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            nvps.add(basicNameValuePair);
        }
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        return httpPost;
    }*/
}






